<?php
/**
 * Created by PhpStorm.
 * User: jacob
 * Date: 18-8-6
 * Time: 下午5:28
 */
namespace Tiny;

class Logger extends Log {

  const error_log = "/soft/php/var/log/error.log";

  public static function init() {
    self::setInstance(new self());
    self::$isInited = true;
    $request = new Request();
    $request->api = $_SERVER['REQUEST_URI'];
    self::$instance->setRequest($request);
  }

  public static function getInstance(): Log {
    if (!self::hasInited()){
      self::init();
    }
    return self::$instance;
  }

  public function trace(string $message, \Throwable $throwable = null) {
    $this->formate($message,self::trace);
  }
  public function debug(string $message, \Throwable $throwable = null) {
    $this->formate($message,self::debug);
  }
  public function info(string $message, \Throwable $throwable = null) {
    $this->formate($message,self::info);
  }
  public function warn(string $message, \Throwable $throwable = null) {
    $this->formate($message,self::warn);
  }
  public function error(string $message, \Throwable $throwable = null) {
    $this->formate($message,self::error);
  }
  public function fatal(string $message, \Throwable $throwable = null) {
    $this->formate($message,self::fatal);
  }

  private function formate(string $message,string $type){
    $data = "[".date("Y-m-d H:i:s")."]"."[".$type."]"."[api:".
      self::$instance->request->api."]".$message."".PHP_EOL;
    file_put_contents(self::error_log,$data,FILE_APPEND);
  }


}